package model;

import java.util.Arrays;
import java.util.List;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import beans.Bookmark;
import beans.User;

/*
 *  Course: S09-15437 - WEB APPLICATION DEVELOPMENT 
 * 	Name: Jiale Huang
 * 	Andrew ID: jialeh
 * 	Date: 2/23/2009
 *  Assignment: Hw4
 */

public class BookmarkDAOFactoryImpl implements BookmarkDAO {

	private BeanFactory<Bookmark> factory;
	
	public BookmarkDAOFactoryImpl() throws DAOException
	{
		try {
			BeanTable<Bookmark> table = BeanTable.getInstance(Bookmark.class,"jialeh_bookmark");
			if (!table.exists()) {
				table.create("id");   // provide the primary key property(s)
			}
			factory = table.getFactory();
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}	
	}	
	
	@Override
	public Bookmark create(Bookmark bookmark) throws DAOException {
		
		try {
			Transaction.begin();
			
			// Get item at bottom of list
			Bookmark[] b = factory.match(MatchArg.max("id"));
			Bookmark bottomBean = (b.length==0 ? null : b[0]);
			
			Bookmark nbookmark;
			if (bottomBean == null) {
				// List is empty...just add it with id=1
				nbookmark = factory.create(1);
			} else {
				// Create the new item with id one less than the top bean's id
				int newId = bottomBean.getId()+1;
				if (newId == 0) newId = 1;  // Don't use zero
				nbookmark = factory.create(newId);
			}
			
			nbookmark.setUserEmail(bookmark.getUserEmail());
			nbookmark.setURL( bookmark.getURL());
			nbookmark.setComment( bookmark.getComment() ); 
			nbookmark.setClickCount( bookmark.getClickCount());
			Transaction.commit();
			return nbookmark;
			
		} catch (DuplicateKeyException e) {
			throw new DAOException("A user with this name already exists.");
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally{
			
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	@Override
	public Bookmark lookup(int id) throws DAOException {

		try {
			return factory.lookup(id);
		} catch (RollbackException e) {
			Transaction.rollback();
			throw new DAOException(e);
		}
	}
	
	@Override
	public List<Bookmark> getUserBookmarks(String userEmail)
			throws DAOException {
		
		try {
			Bookmark[] temp = factory.match(MatchArg.equalsIgnoreCase("userEmail", userEmail));
			List<Bookmark> result = Arrays.asList(temp);
			return result;
		} catch (RollbackException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean updateClickCount(Bookmark bookmark)
			throws DAOException {
		
		try{			
			Transaction.begin();
			Bookmark rBookmark = factory.lookup(bookmark.getId());
			rBookmark.setClickCount(rBookmark.getClickCount() + 1);
			Transaction.commit();
			
			return true;
		}catch (RollbackException e) {
			throw new DAOException(e);				
		}finally{
			
			if (Transaction.isActive()){
				Transaction.rollback();
				return false;
			}
		}
	}
	
	@Override
	public boolean delBookmark(int id) throws DAOException{
		
		try{
			Transaction.begin();
			factory.delete(id);
			Transaction.commit();
			
			return true;
		}catch(RollbackException e){
			throw new DAOException(e);
		}finally{
			if(Transaction.isActive()){
				Transaction.rollback();
				return false;
			}
		}
		
	}
}
